<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 压缩字符串 -->
    <script>
      var compress = function (chars) {
        let write = 0,
          read,
          tcnt = 1 //注意这里从1开始
        for (read = 0; read < chars.length; read++) {
          if (read === chars.length - 1 || chars[read] !== chars[read + 1]) {
            chars[write++] = chars[read]
            if (tcnt > 1) {
              tcnt = tcnt + ''
              for (let s of tcnt) {
                chars[write++] = s
              }
            }
            tcnt = 0
          }
          tcnt++
        }
        return write
      }
      console.log(compress(['a', 'a', 'b', 'b', 'c', 'c', 'c']))
    </script>
  </body>
</html>
